package com.imut.lagain.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imut.lagain.entity.AnxietyReminder;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * 焦虑提醒数据访问层
 */
@Repository
public interface AnxietyReminderRepository extends BaseMapper<AnxietyReminder> {

    /**
     * 根据用户ID查询焦虑提醒记录
     *
     * @param userId 用户ID
     * @return 焦虑提醒记录列表
     */
    @Select("SELECT * FROM anxiety_reminder WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY created_at DESC")
    List<AnxietyReminder> selectByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID查询启用的焦虑提醒
     *
     * @param userId 用户ID
     * @return 启用的焦虑提醒记录列表
     */
    @Select("SELECT * FROM anxiety_reminder WHERE user_id = #{userId} AND is_enabled = 1 AND is_deleted = 0 ORDER BY time_of_day ASC")
    List<AnxietyReminder> selectEnabledByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID和提醒类型查询焦虑提醒
     *
     * @param userId 用户ID
     * @param reminderType 提醒类型
     * @return 焦虑提醒记录列表
     */
    @Select("SELECT * FROM anxiety_reminder WHERE user_id = #{userId} AND reminder_type = #{reminderType} AND is_deleted = 0 ORDER BY created_at DESC")
    List<AnxietyReminder> selectByUserIdAndReminderType(@Param("userId") Long userId, @Param("reminderType") String reminderType);

    /**
     * 根据提醒ID查询焦虑提醒
     *
     * @param reminderId 提醒ID
     * @return 焦虑提醒记录
     */
    @Select("SELECT * FROM anxiety_reminder WHERE id = #{reminderId} AND is_deleted = 0")
    AnxietyReminder selectByReminderId(@Param("reminderId") Long reminderId);

    /**
     * 查询需要触发的焦虑提醒
     *
     * @param currentTime 当前时间
     * @return 需要触发的焦虑提醒列表
     */
    @Select("SELECT * FROM anxiety_reminder WHERE is_enabled = 1 AND next_trigger <= #{currentTime} AND is_deleted = 0")
    List<AnxietyReminder> selectTriggeredReminders(@Param("currentTime") LocalDateTime currentTime);

    /**
     * 查询特定时间点的焦虑提醒
     *
     * @param timeOfDay 时间点
     * @return 特定时间点的焦虑提醒列表
     */
    @Select("SELECT * FROM anxiety_reminder WHERE is_enabled = 1 AND time_of_day = #{timeOfDay} AND is_deleted = 0")
    List<AnxietyReminder> selectByTimeOfDay(@Param("timeOfDay") LocalTime timeOfDay);

    /**
     * 更新提醒触发信息
     *
     * @param reminderId 提醒ID
     * @param lastTriggered 最后一次触发时间
     * @param nextTrigger 下一次触发时间
     * @param updatedAt 更新时间
     * @return 更新记录数
     */
    @Update("UPDATE anxiety_reminder SET last_triggered = #{lastTriggered}, next_trigger = #{nextTrigger}, trigger_count = trigger_count + 1, updated_at = #{updatedAt} WHERE id = #{reminderId}")
    int updateTriggerInfo(@Param("reminderId") Long reminderId, 
                         @Param("lastTriggered") LocalDateTime lastTriggered, 
                         @Param("nextTrigger") LocalDateTime nextTrigger, 
                         @Param("updatedAt") LocalDateTime updatedAt);

    /**
     * 增加提醒响应计数
     *
     * @param reminderId 提醒ID
     * @param updatedAt 更新时间
     * @return 更新记录数
     */
    @Update("UPDATE anxiety_reminder SET response_count = response_count + 1, updated_at = #{updatedAt} WHERE id = #{reminderId}")
    int incrementResponseCount(@Param("reminderId") Long reminderId, @Param("updatedAt") LocalDateTime updatedAt);

    /**
     * 更新提醒效果评分
     *
     * @param reminderId 提醒ID
     * @param effectivenessRating 效果评分
     * @param updatedAt 更新时间
     * @return 更新记录数
     */
    @Update("UPDATE anxiety_reminder SET effectiveness_rating = #{effectivenessRating}, updated_at = #{updatedAt} WHERE id = #{reminderId}")
    int updateEffectivenessRating(@Param("reminderId") Long reminderId, 
                               @Param("effectivenessRating") Double effectivenessRating, 
                               @Param("updatedAt") LocalDateTime updatedAt);

    /**
     * 启用或禁用提醒
     *
     * @param reminderId 提醒ID
     * @param isEnabled 是否启用
     * @param updatedAt 更新时间
     * @return 更新记录数
     */
    @Update("UPDATE anxiety_reminder SET is_enabled = #{isEnabled}, updated_at = #{updatedAt} WHERE id = #{reminderId}")
    int updateReminderStatus(@Param("reminderId") Long reminderId, 
                            @Param("isEnabled") Boolean isEnabled, 
                            @Param("updatedAt") LocalDateTime updatedAt);

    /**
     * 软删除提醒
     *
     * @param reminderId 提醒ID
     * @param deletedAt 删除时间
     * @return 删除记录数
     */
    @Update("UPDATE anxiety_reminder SET is_deleted = 1, deleted_at = #{deletedAt} WHERE id = #{reminderId}")
    int softDelete(@Param("reminderId") Long reminderId, @Param("deletedAt") LocalDateTime deletedAt);

    /**
     * 统计用户提醒数量
     *
     * @param userId 用户ID
     * @return 用户提醒数量
     */
    @Select("SELECT COUNT(*) FROM anxiety_reminder WHERE user_id = #{userId} AND is_deleted = 0")
    Long countByUserId(@Param("userId") Long userId);

    /**
     * 统计用户启用的提醒数量
     *
     * @param userId 用户ID
     * @return 用户启用的提醒数量
     */
    @Select("SELECT COUNT(*) FROM anxiety_reminder WHERE user_id = #{userId} AND is_enabled = 1 AND is_deleted = 0")
    Long countEnabledByUserId(@Param("userId") Long userId);

    /**
     * 查询用户最近触发的提醒
     *
     * @param userId 用户ID
     * @return 最近触发的提醒
     */
    @Select("SELECT * FROM anxiety_reminder WHERE user_id = #{userId} AND last_triggered IS NOT NULL AND is_deleted = 0 ORDER BY last_triggered DESC LIMIT 1")
    AnxietyReminder findLatestTriggeredByUserId(@Param("userId") Long userId);

    /**
     * 分页查询用户焦虑提醒
     *
     * @param page 分页信息
     * @param userId 用户ID
     * @return 分页查询结果
     */
    IPage<AnxietyReminder> selectPageByUserId(Page<AnxietyReminder> page, @Param("userId") Long userId);
}